﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

using DBMS.Models;
using DBMS.Helpers.Metadata;

namespace DBMS.Helpers.Application
{
    public sealed class Configuration
    {
        private static volatile Configuration instance;
        private static object syncRoot = new Object();

        private static string DIRNAME_DATABASES = "databases";

        private String cwd;
        public String WorkingDirectory { get { return cwd; } }

        public static Configuration Instance
        {
            get
            {
                if (instance == null) 
                {
                    lock (syncRoot) 
                    {
                       if (instance == null) 
                          instance = new Configuration();
                    }
                 }
            return instance;
            }
        }


        public String GetDatabaseFolder(Database db)
        {
            return WorkingDirectory + "\\" + db.Index.ToString();
        }

        public String GetDatabaseFolder(int dbIndex)
        {
            return WorkingDirectory + "\\" + dbIndex.ToString();
        }

        public String GetTableFolder(Database db, Table table)
        {
            return GetDatabaseFolder(db) + "\\" + table.Index.ToString();
        }

        public String GetTableFolder(int dbIndex, int tableIndex)
        {
            return WorkingDirectory + "\\" + dbIndex.ToString() + "\\" + tableIndex.ToString();
        }

        private Configuration()
        {
            cwd = AppDomain.CurrentDomain.BaseDirectory + @"\" + DIRNAME_DATABASES;
            if (!Directory.Exists(cwd))
            {
                Directory.CreateDirectory(cwd);
            }
        }
    }
}